<template>
  <section class="todoapp">
    <TodoHeader @create="createFn" :arr="list"   ></TodoHeader>
    <TodoMain :arr="showArr" @click="deleteFn"></TodoMain>
    <TodoFooter :farr="showArr" @chengeType="typeFn" @clear="clearFun"></TodoFooter>
  </section>
</template>
<script>
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  data() {
    return {
      list:JSON.parse(localStorage.getItem('todoList')) || [],
      // list: [
      //   { id: 100, name: "吃饭", isDone: true },
      //   { id: 201, name: "睡觉", isDone: false },
      //   { id: 103, name: "打豆豆", isDone: true }
      // ],
      getSel:"all"
    };
  },
  watch:{
    list:{
      deep:true,
      hendler(){
         localStorage.setItem('todoList', JSON.stringify(this.list))
      }
    }
  },
  methods: {
    deleteFn(theId){ // 删除任务
      let index = this.list.findIndex(obj => obj.id === theId)
      this.list.splice(index, 1)
    },
    createFn(taskName) {
      let id =
        this.list.length == 0 ? 100 : this.list[this.list.length - 1].id + 1;
      this.list.push({
        id: id,
        name: taskName,
        isDone: false
      });
    },
    typeFn(str){
      this.getSel=str
    },
    clearFun(){
      this.list=this.list.filter(obj=>obj.isDone==false)
    }
  
  },
  computed:{
    showArr(){
      if(this.getSel==='yes'){
        return this.list.filter(obj=>obj.isDone===true)
      }else if (this.getSel==='no'){
        return this.list.filter(obj=>obj.isDone===false)
      }else{
        return this.list
      }
    }
  }
  
};
</script>
<style>
</style>